package com.singfan.script;

import com.singfan.core.dao.database.IShopHairAppointmentSummaryDao;
import com.singfan.core.dao.database.IShopHairDao;
import com.singfan.core.dao.database.model.ShopHairAppointmentSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 门店每天预约时间汇总的记录的生产脚本
 * Author: HanSongHe
 * Date: 2015-09-04 23:50
 * Desc: 由系统的crontab每天凌晨1点执行一次，生产未来七天内尚未生成的数据。
 */
@Component
public class ShopAppointmentSummaryCreateScript extends ScriptRunner {
    private static final int DAY = 24 * 60 * 60 * 1000;
    @Autowired
    private IShopHairDao shopHairDao;
    @Autowired
    private IShopHairAppointmentSummaryDao shopHairAppointmentSummaryDao;

    @Override
    public void run(List<String> args) {
        List<Long> shopIdList = shopHairDao.selectShopIdList();
        long today = System.currentTimeMillis();
        for (Long shopId : shopIdList){
            int maxAppointmentDay = shopHairAppointmentSummaryDao.selectMaxAppointmentDay(shopId);
            int todayAppointment = getAppointmentDay(new Date());
            int futureAppointment = getAppointmentDay(new Date(today + DAY * 7));

            int startAppointment = 0;
            int endAppointment = 0;
            long startDateTime = 0;
            if (maxAppointmentDay == 0){
                startDateTime = System.currentTimeMillis();
                startAppointment = todayAppointment;
                endAppointment = futureAppointment;
            }
            else if (maxAppointmentDay >= todayAppointment && maxAppointmentDay < futureAppointment){
                int year = maxAppointmentDay / 10000;
                int month = ((maxAppointmentDay % 10000) / 100) - 1;
                int day = maxAppointmentDay % 100;
                long maxAppointmentTime = new Date(year - 1900, month, day).getTime();

                startDateTime = maxAppointmentTime + DAY;
                startAppointment = getAppointmentDay(new Date(startDateTime));
                endAppointment = futureAppointment;
            }

            if (startAppointment > 0){
                String appointmentDayList = "[";
                int appointmentDayCount = 0;
                while (endAppointment >= startAppointment){
                    appointmentDayCount++;
                    appointmentDayList += startAppointment + ",";
                    ShopHairAppointmentSummary shopHairAppointmentSummary = new ShopHairAppointmentSummary();
                    shopHairAppointmentSummary.setShopId(shopId);
                    shopHairAppointmentSummary.setAppointmentDay(startAppointment);
                    shopHairAppointmentSummary.setCreateTime(new Date());
                    shopHairAppointmentSummary.setUpdateTime(shopHairAppointmentSummary.getCreateTime());

                    shopHairAppointmentSummaryDao.create(shopHairAppointmentSummary);

                    startDateTime += DAY;
                    startAppointment = getAppointmentDay(new Date(startDateTime));
                }
                appointmentDayList += "]";

                System.out.println(String.format("shop(%1$d) create %2$d appointments %3$s !", shopId, appointmentDayCount, appointmentDayList));

                System.out.println("================================================================");
            }
        }
    }

    private static int getAppointmentDay(Date date){
        int month = date.getMonth() + 1;
        int day = date.getDate();
        String monthText = fill0(month);
        String dayText = fill0(day);

        return Integer.parseInt(String.format("%1$d%2$s%3$s", date.getYear() + 1900, monthText, dayText));
    }

    private static String fill0(int value){
        String result;

        if (value < 10){
            result = "0" + value;
        }
        else{
            result = String.valueOf(value);
        }

        return result;
    }
}
